/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manage_image;

import java.io.File;
import Manager_Image_Utils.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import static java.awt.Frame.HAND_CURSOR;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author PHONG
 */
public class Main extends javax.swing.JFrame {

    private int _height = 50;
    private int _width = 50;
    private float zoom = 1;
    private int angle = 0;
    private int imgW, imgH;// 
    private int thumainlindex;
    private int thumnail_X;
    private int thumainl_Y;
    private JPanel thumbnailbottom = new JPanel();
    private JPanel MainShow = new JPanel();
    private File FilePath = new File("E://img//");
    private int index = 0;
    private int indexold = 0;
    private JLabel imgmain = new JLabel();
    private JScrollPane viewframe;
    private JList jListAbum;
    private JScrollPane JScrollAbum;
    boolean flag = false;
    private Dimension screenSize;
    private Color Bg = Color.black;
    private int WindownW, WindownH;
    ArrayList<Abum> AbumsList;
    private File currentfile;
    private Thread play;
    Thread ThreadDrawImage;
    Thread drawTree;
    Thread fullscreen;
    Thread ThreadDrawThumbnail;
    Thread ThreadLoadAbums;
    Thread ControlerThread;
    Thread mainthread;
    boolean ischangePath = true;
    ArrayList<Thread> ArrayThread = new ArrayList<Thread>();
    public ArrayList<File> findimage;
    private Desktop _desktop;
    private FileSystemView _fileSystemView;
    private DefaultTreeModel _treeModel;


    /**
     * Creates new form Main
     */
    protected int ColumThumbnail() {
        int col = (int) thumbnailbottom.getComponents().length;
        return col;
    }

    private void setDefaultValueOnLoad() {
//        screen.setOrientation(JSplitPane.VERTICAL_SPLIT);
//        screen.setDividerLocation(0.9);
//        screen.setBorder(null);
//        
//        screenmain.setRightComponent(screen);
        screen.setRightComponent(new JScrollPane(thumbnailbottom, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

        thumnail_X = this.thumbnailbottom.getY();
        thumainl_Y = this.thumbnailbottom.getX();
        screen.setLeftComponent(viewframe);
        imgH = Constants.img_H;
        imgW = Constants.img_W;
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        WindownW = (int) Math.ceil(screenSize.getWidth());
        WindownH = (int) Math.ceil(screenSize.getHeight()) - 30;
        screen.setSize(screen.getWidth(), screenLeftMain.getHeight());
    }

    public Main() {
        initComponents();
        getfile(false, null);
        setDefaultValueOnLoad();
        ThreadLoadAbums = new Thread() {
            public void run() {
                try {
                        LoadAbums();
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        };
        ThreadLoadAbums.start();
        ArrayThread.add(ThreadLoadAbums);
         jListAbums.addMouseListener(new MouseAdapter() {
           
            public void mousePressed(MouseEvent ev) {
                Integer i = jListAbums.getSelectedIndex();
                i = jListAbums.getSelectedIndex();
                Abum ab = AbumsList.get(i);
                if (ab != null) {
                    FilePath = ab.getFdir();
                    
                    Thread drawthum = new Thread() {
                        public void run() {
                            try {
                                ShowImageInToThumnail(FilePath);
                                DrawImage();
                            } catch (IOException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    };
                    drawthum.start();
                    ArrayThread.add(drawthum);
                }
                if (SwingUtilities.isRightMouseButton(ev)) {
                    PopupMenuAbum menutrip;
                    menutrip = new PopupMenuAbum(ab.getFdir());
                    
                    menutrip.show(ev.getComponent(), ev.getX(), ev.getY());
                }
                return;
            }
        });
//        drawTree = new Thread() {
//            public void run() {
//                CreateJtree();
//            }
//        };
//        drawTree.start();
//        ArrayThread.add(drawTree);
        ThreadDrawImage = new Thread() {
            public void run() {
                DrawImage();
            }
        };
        ThreadDrawImage.start();
        ArrayThread.add(ThreadDrawImage);
        mainthread = new Thread() {
            public void run() {
                while (ArrayThread.size() >= 10) {
                    for (int i = 0; i < ArrayThread.size() / 2; i++) {
                        ArrayThread.get(i).interrupt();
                        ArrayThread.remove(i);
                    }
                }
            }
        };
        mainthread.start();
        ArrayThread.add(mainthread);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        btnZoomOut = new javax.swing.JButton();
        btnZoomIn = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnRotate_Unclockwise = new javax.swing.JButton();
        btnRotate_clockwise = new javax.swing.JButton();
        btnPlay = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jbtnEmail = new javax.swing.JButton();
        jbtnPrint = new javax.swing.JButton();
        btnFullScreen = new javax.swing.JButton();
        btnFullScreenPressKey = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnRefeshAbum = new javax.swing.JButton();
        btnNewAbum = new javax.swing.JButton();
        screenmain = new javax.swing.JSplitPane();
        screenLeft = new javax.swing.JScrollPane();
        screenLeftMain = new javax.swing.JSplitPane();
        screenLeftMainTop = new javax.swing.JScrollPane();
        jListAbums = new javax.swing.JList();
        screenLeftMainBottom = new javax.swing.JScrollPane();
        Jtreedisk = new javax.swing.JTree();
        screen = new javax.swing.JSplitPane();
        txtKeySearche = new java.awt.TextField();
        jbtnTimkiem = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jmnnewalbum = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jmnimport = new javax.swing.JMenuItem();
        jmnprint = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jmnselectall = new javax.swing.JMenu();
        jmncat = new javax.swing.JMenuItem();
        jmncopy = new javax.swing.JMenuItem();
        jmndelete = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jmnFulscreen = new javax.swing.JMenuItem();
        jmnsendEmail = new javax.swing.JMenuItem();
        jMenu8 = new javax.swing.JMenu();
        jmnabout = new javax.swing.JMenuItem();

        jMenu1.setText("jMenu1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        btnZoomOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/button_zoom_in.png"))); // NOI18N
        btnZoomOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomOutActionPerformed(evt);
            }
        });

        btnZoomIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/button_zoom_out.png"))); // NOI18N
        btnZoomIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomInActionPerformed(evt);
            }
        });

        btnPrevious.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Actions-media-skip-backward-icon.png"))); // NOI18N
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Actions-media-skip-forward-icon.png"))); // NOI18N
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnRotate_Unclockwise.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/shape-rotate-anticlockwise-icon.png"))); // NOI18N
        btnRotate_Unclockwise.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRotate_UnclockwiseActionPerformed(evt);
            }
        });

        btnRotate_clockwise.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/shape-rotate-clockwise-icon.png"))); // NOI18N
        btnRotate_clockwise.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRotate_clockwiseActionPerformed(evt);
            }
        });

        btnPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Alarm-Play-icon.png"))); // NOI18N
        btnPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayActionPerformed(evt);
            }
        });

        btnStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Pause-Disabled-icon.png"))); // NOI18N
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        jbtnEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/mail-send-icon.png"))); // NOI18N
        jbtnEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEmailActionPerformed(evt);
            }
        });

        jbtnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/print-icon.png"))); // NOI18N
        jbtnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPrintActionPerformed(evt);
            }
        });

        btnFullScreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Folders-OS-Full-Screen-Metro-icon.png"))); // NOI18N
        btnFullScreen.setText("Full sreen slide show");
        btnFullScreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFullScreenActionPerformed(evt);
            }
        });

        btnFullScreenPressKey.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Folders-OS-Full-Screen-Metro-icon.png"))); // NOI18N
        btnFullScreenPressKey.setText("Full sreen");
        btnFullScreenPressKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFullScreenPressKeyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(538, Short.MAX_VALUE)
                .addComponent(btnFullScreenPressKey)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnFullScreen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnPrint)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnEmail)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnZoomOut)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnZoomIn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPrevious)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnStop)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPlay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRotate_Unclockwise)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRotate_clockwise)
                .addGap(62, 62, 62))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jbtnEmail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                                .addComponent(btnZoomOut, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnZoomIn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnPrevious, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnNext, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRotate_Unclockwise, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnPlay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnStop, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbtnPrint, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnFullScreen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(btnRotate_clockwise, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 1, Short.MAX_VALUE))
                    .addComponent(btnFullScreenPressKey, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setText("jLabel1");

        btnRefeshAbum.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/refresh-small-icon.png"))); // NOI18N
        btnRefeshAbum.setText("Refresh");
        btnRefeshAbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefeshAbumActionPerformed(evt);
            }
        });

        btnNewAbum.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Search-Images-icon.png"))); // NOI18N
        btnNewAbum.setText("New Allbum");
        btnNewAbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewAbumActionPerformed(evt);
            }
        });

        screenmain.setDividerSize(2);
        screenmain.setResizeWeight(0.2);

        screenLeftMain.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        screenLeftMain.setResizeWeight(0.5);

        jListAbums.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        screenLeftMainTop.setViewportView(jListAbums);

        screenLeftMain.setTopComponent(screenLeftMainTop);

        Jtreedisk.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                JtreediskTreeExpanded(evt);
            }
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
            }
        });
        Jtreedisk.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                JtreediskValueChanged(evt);
            }
        });
        screenLeftMainBottom.setViewportView(Jtreedisk);

        screenLeftMain.setRightComponent(screenLeftMainBottom);

        screenLeft.setViewportView(screenLeftMain);

        screenmain.setLeftComponent(screenLeft);

        screen.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        screen.setResizeWeight(0.85);
        screen.setEnabled(false);
        screenmain.setRightComponent(screen);

        txtKeySearche.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKeySearcheActionPerformed(evt);
            }
        });

        jbtnTimkiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/find.png"))); // NOI18N
        jbtnTimkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnTimkiemActionPerformed(evt);
            }
        });

        jMenu2.setText("File");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jmnnewalbum.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Search-Images-icon.png"))); // NOI18N
        jmnnewalbum.setText("New Album...");
        jmnnewalbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnnewalbumActionPerformed(evt);
            }
        });
        jMenu2.add(jmnnewalbum);
        jMenu2.add(jSeparator1);

        jmnimport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Import-icon.png"))); // NOI18N
        jmnimport.setText("Import File To Album From...");
        jmnimport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnimportActionPerformed(evt);
            }
        });
        jMenu2.add(jmnimport);

        jmnprint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/print-icon.png"))); // NOI18N
        jmnprint.setText("Print");
        jmnprint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnprintActionPerformed(evt);
            }
        });
        jMenu2.add(jmnprint);

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/exit.png"))); // NOI18N
        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        jmnselectall.setText("Edit");
        jmnselectall.setMargin(new java.awt.Insets(0, 10, 0, 0));

        jmncat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Cut-icon.png"))); // NOI18N
        jmncat.setText("Cut");
        jmncat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmncatActionPerformed(evt);
            }
        });
        jmnselectall.add(jmncat);

        jmncopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Copy-icon.png"))); // NOI18N
        jmncopy.setText("Copy");
        jmnselectall.add(jmncopy);

        jmndelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/delete.png"))); // NOI18N
        jmndelete.setText("Delete");
        jmndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmndeleteActionPerformed(evt);
            }
        });
        jmnselectall.add(jmndelete);

        jMenuItem1.setText("Select All...");
        jmnselectall.add(jMenuItem1);

        jMenuBar1.add(jmnselectall);

        jMenu5.setText("Picture");
        jMenu5.setMargin(new java.awt.Insets(0, 0, 0, 10));

        jmnFulscreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/Folders-OS-Full-Screen-Metro-icon.png"))); // NOI18N
        jmnFulscreen.setText("FullScreen");
        jmnFulscreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnFulscreenActionPerformed(evt);
            }
        });
        jMenu5.add(jmnFulscreen);

        jmnsendEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageIcons/mail-send-icon.png"))); // NOI18N
        jmnsendEmail.setText("Send Email");
        jmnsendEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnsendEmailActionPerformed(evt);
            }
        });
        jMenu5.add(jmnsendEmail);

        jMenuBar1.add(jMenu5);

        jMenu8.setText("Helps");
        jMenu8.setMargin(new java.awt.Insets(0, 0, 0, 10));

        jmnabout.setText("About");
        jmnabout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnaboutActionPerformed(evt);
            }
        });
        jMenu8.add(jmnabout);

        jMenuBar1.add(jMenu8);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(screenmain, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnRefeshAbum, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNewAbum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtKeySearche, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRefeshAbum)
                            .addComponent(btnNewAbum))
                        .addComponent(txtKeySearche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnTimkiem)
                        .addGap(3, 3, 3)))
                .addComponent(screenmain, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmncatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmncatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jmncatActionPerformed

    void getfile(Boolean check, File filechooser) {

        File[] test = null;


        if (check == false) {
            _fileSystemView = FileSystemView.getFileSystemView();
        }


        _desktop = Desktop.getDesktop();

        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        _treeModel = new DefaultTreeModel(root);



        // show the file system roots.
        File[] roots;
        if (check == false) {
            roots = _fileSystemView.getRoots();
            for (File fileSystemRoot : roots) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
                root.add(node);
                File[] files = _fileSystemView.getFiles(fileSystemRoot, true);
                for (File file : files) {
                    if (file.isDirectory()) {

                        node.add(new DefaultMutableTreeNode(file));
                    }
                }
                //
            }
        } else {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(filechooser);
            root.add(node);
            File[] files = _fileSystemView.getFiles(filechooser, true);
            for (File file : files) {
                if (file.isDirectory()) {
                    node.add(new DefaultMutableTreeNode(file));
                }
            }
        }
        Jtreedisk.setModel(_treeModel);
        Jtreedisk.setRootVisible(false);
        Jtreedisk.setCellRenderer(new RenderTree());
        Jtreedisk.expandRow(0);
    }

    private void showChildren(final DefaultMutableTreeNode node) {
        Jtreedisk.setEnabled(false);

        SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
            @Override
            public Void doInBackground() {
                File file = (File) node.getUserObject();
                if (file.isDirectory()) {
                    File[] files = _fileSystemView.getFiles(file, true); //!!
                    if (node.isLeaf()) {
                        for (File child : files) {
                            if (child.isDirectory()) {
                                publish(child);
                            }
                        }
                    }

                }
                return null;
            }

            @Override
            protected void process(List<File> chunks) {
                for (File child : chunks) {
                    node.add(new DefaultMutableTreeNode(child));
                }
            }

            @Override
            protected void done() {

                Jtreedisk.setEnabled(true);
            }
        };
        worker.execute();
    }
    private void btnNewAbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewAbumActionPerformed


        CreateAbum f = new CreateAbum(this, true);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        if (f.iscreate == 1) {
            try {
                LoadAbums();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnNewAbumActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void btnRefeshAbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefeshAbumActionPerformed
        Thread t=new Thread(){
            public void run(){
                try {
            
                    LoadAbums();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        t.start();
        ArrayThread.add(t);
        
    }//GEN-LAST:event_btnRefeshAbumActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
    }//GEN-LAST:event_formComponentResized

    private void btnZoomInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomInActionPerformed
        // TODO add your handling code here:
        angle = 0;
        ZoomIn();
    }//GEN-LAST:event_btnZoomInActionPerformed

    private void btnZoomOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomOutActionPerformed
        // TODO add your handling code here:
        angle = 0;
        ZoomOut();
    }//GEN-LAST:event_btnZoomOutActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        // TODO add your handling code here:
        Previous();
    }//GEN-LAST:event_btnPreviousActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        Next();
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        // TODO add your handling code here:
        if (flag) {
            play.interrupt();
            flag = false;
        }
    }//GEN-LAST:event_btnStopActionPerformed

    private void btnRotate_UnclockwiseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRotate_UnclockwiseActionPerformed
        // TODO add your handling code here:
        zoom = 1;
        RotateAntiClockwise();
    }//GEN-LAST:event_btnRotate_UnclockwiseActionPerformed

    private void btnRotate_clockwiseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRotate_clockwiseActionPerformed
        // TODO add your handling code here:
        zoom = 1;
        RotateClockwise();
    }//GEN-LAST:event_btnRotate_clockwiseActionPerformed

    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed
        // TODO add your handling code here:
        if (!flag) {
            play = new Thread() {
                public void run() {
                    while (flag) {
                        Next();
                        try {
                            if (flag) {
                                play.sleep(3000);
                            }
                        } catch (InterruptedException ex) {
                            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            };
            play.start();
            ArrayThread.add(play);
            flag = true;
        }
    }//GEN-LAST:event_btnPlayActionPerformed

    private void jbtnEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEmailActionPerformed
        // TODO add your handling code here:= new 
        frmsentemail frm = new frmsentemail(null, true);
        frm.setLocation(300, 200);
        String pathname = currentfile.getPath();
        frm.setPath(pathname);


        frm.setVisible(true);

    }//GEN-LAST:event_jbtnEmailActionPerformed

    private void jbtnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPrintActionPerformed
        // TODO add your handling code here:
        MediaTracker tracker = new MediaTracker(new JPanel());

        try {
            if (currentfile == null) {
                return;
            }
            Image img = ImageIO.read(currentfile);
            tracker.addImage(img, 1);
            tracker.waitForAll();
            Library.print(img);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jbtnPrintActionPerformed

    private void btnFullScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFullScreenActionPerformed
        // TODO add your handling code here:
        // full screen
        
        FullScreen f;
        ArrayList<File> listFile=new ArrayList<File>();
        for(int i=0;i<thumbnailbottom.getComponentCount();i++)
        {
            ImageThumbnail it=(ImageThumbnail)thumbnailbottom.getComponent(i);
            if(it!=null)
            {
                listFile.add(it.getF());
            }
        }
        if(listFile.size()<=0)
            return;
        f = new FullScreen(listFile);
    }//GEN-LAST:event_btnFullScreenActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowActivated

    private void JtreediskValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_JtreediskValueChanged
        // TODO add your handling code here:
        //System.out.print(evt.getOldLeadSelectionPath().toString());
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
        showChildren(node);
        if (Jtreedisk.getLastSelectedPathComponent() == null) {
            return;
        }
        final String path = Jtreedisk.getLastSelectedPathComponent().toString();
        final File fs = new File(path);
        if (fs != null) {
            if (fs.isDirectory()) {
                if (fs != FilePath) {
                    Thread a = new Thread() {
                        public void run() {
                            try {
                                FilePath = fs;
                                ShowImageInToThumnail(FilePath);
                            } catch (IOException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    };
                    a.start();
                    ArrayThread.add(a);
                }
            }

        }
        Jtreedisk.setCellRenderer(new RenderTree());
    }//GEN-LAST:event_JtreediskValueChanged

    private void JtreediskTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_JtreediskTreeExpanded
        // TODO add your handling code here:
    }//GEN-LAST:event_JtreediskTreeExpanded

    private void btnFullScreenPressKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFullScreenPressKeyActionPerformed
        FullScreen f;
        ArrayList<File> listFile=new ArrayList<File>();
        for(int i=0;i<thumbnailbottom.getComponentCount();i++)
        {
            ImageThumbnail it=(ImageThumbnail)thumbnailbottom.getComponent(i);
            if(it!=null)
            {
                listFile.add(it.getF());
            }
        }
        if(listFile.size()<=0)
            return;
        try {
            new FullScreenPressKey(listFile).setVisible(true);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnFullScreenPressKeyActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        // interrup thread
        for (Thread t : ArrayThread) {
            t.interrupt();
        }
    }//GEN-LAST:event_formWindowClosing

    private void jbtnTimkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimkiemActionPerformed
        // TODO add your handling code here:
        String filename = txtKeySearche.getText();
        final String path = Jtreedisk.getLastSelectedPathComponent().toString();
        findimage= new ArrayList<File>();
        if (path.isEmpty() == true) {
            Findimage(new File("C:\\"), filename);

        } else {
            Findimage(new File(path), filename);
        }
        try {
            ShowImageFindInToThumnail(findimage);
            DrawImage();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }




    }//GEN-LAST:event_jbtnTimkiemActionPerformed

    private void Findimage(File dir, String filename) {
        File[] listOfFiles = dir.listFiles();
        if (listOfFiles == null) {
            return;
        }
        int flag = 0;
        //String Exention[] = filename.split('.');
        int dot = filename.lastIndexOf('.');
        String name = (dot == -1) ? filename : filename.substring(0, dot);
        String extension = (dot == -1) ? "" : filename.substring(dot + 1, filename.length());
        if (!name.isEmpty() && new Library().CheckExtensions(extension)) {

            flag = 1;

        } else if (name.isEmpty() && new Library().CheckExtensions(extension))// neu chi co phan mo rong ko co ten file
        {
            flag = 2;
        }
        if (flag == 1) {// tim kiem theo ten va phan mo rong
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    String _name = listOfFiles[i].getName();
                    if (_name.equals(filename)) {
                        findimage.add(listOfFiles[i]);
                       
                    }

                } else if (listOfFiles[i].isDirectory()) {
                    Findimage(listOfFiles[i], filename);

                }
            }
        } else if (flag == 2)// tim kiem theo phan mo rong
        {
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    String _name = listOfFiles[i].getName();
                    if (_name.contains(extension)) {
                         findimage.add(listOfFiles[i]);
                      
                    }

                } else if (listOfFiles[i].isDirectory()) {
                    Findimage(listOfFiles[i], filename);
                }
            }
        }

    }
    private void txtKeySearcheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtKeySearcheActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtKeySearcheActionPerformed

    private void jmnnewalbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnnewalbumActionPerformed
        CreateAbum f = new CreateAbum(this, true);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        if (f.iscreate == 1) {
            try {
                LoadAbums();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jmnnewalbumActionPerformed

    private void jmnprintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnprintActionPerformed
        // TODO add your handling code here:
        MediaTracker tracker = new MediaTracker(new JPanel());

        try {
            if (currentfile == null) {
                return;
            }
            Image img = ImageIO.read(currentfile);
            tracker.addImage(img, 1);
            tracker.waitForAll();
            Library.print(img);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jmnprintActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void jmnsendEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnsendEmailActionPerformed
        // TODO add your handling code here:
        frmsentemail frm = new frmsentemail(null, true);
        frm.setLocation(300, 200);
        String pathname = currentfile.getPath();
        frm.setPath(pathname);


        frm.setVisible(true);
    }//GEN-LAST:event_jmnsendEmailActionPerformed

    private void jmnFulscreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnFulscreenActionPerformed
        // TODO add your handling code here:
        ArrayList<File> listFile=new ArrayList<File>();
        for(int i=0;i<thumbnailbottom.getComponentCount();i++)
        {
            ImageThumbnail it=(ImageThumbnail)thumbnailbottom.getComponent(i);
            if(it!=null)
            {
                listFile.add(it.getF());
            }
        }
        if(listFile.size()<=0)
            return;
        try {
            new FullScreenPressKey(listFile).setVisible(true);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jmnFulscreenActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jmndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmndeleteActionPerformed
        // TODO add your handling code here:
        if (Library.DeleteFile(currentfile))
            JOptionPane.showConfirmDialog(null, "xóa file thành công", "Thông báo", JOptionPane.CLOSED_OPTION);
        else
            JOptionPane.showConfirmDialog(null, "xóa file không thành công", "Thông báo", JOptionPane.CLOSED_OPTION);
    
        
    }//GEN-LAST:event_jmndeleteActionPerformed

    private void jmnimportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnimportActionPerformed
        // TODO add your handling code here:
         PopupMenuAbum _import= new PopupMenuAbum(FilePath);
         _import.Import();
    }//GEN-LAST:event_jmnimportActionPerformed

    private void jmnaboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnaboutActionPerformed
        // TODO add your handling code here:
        frmabout frm= new frmabout(null,true);
         frm.setLocation(200, 100);
        frm.setVisible(true);
       
    }//GEN-LAST:event_jmnaboutActionPerformed

    protected void UpdateCurentFile() {
        if (thumbnailbottom.getComponents().length == 0) {
            return;
        }
        final ImageThumbnail lb = (ImageThumbnail) (thumbnailbottom.getComponent(index));
        if (lb == null) {
            return;
        }
        currentfile = lb.getF();
        ImageThumbnail lbold = (ImageThumbnail) thumbnailbottom.getComponent(indexold);
        lbold.setBorder(Constants.borderUnSelected);
        lb.setBorder(Constants.borderSelected);
    }

    protected void DrawImage() {
        Thread t = new Thread() {
            public void run() {
                if (thumbnailbottom.getComponents().length == 0) {
                    return;
                }
                final ImageThumbnail lb = (ImageThumbnail) (thumbnailbottom.getComponent(index));
                if (lb == null) {
                    return;
                }
                JScrollPane _scroll = (JScrollPane) screen.getRightComponent();
                if (_scroll != null) {
                    _scroll.getHorizontalScrollBar().setValue(index * lb.getWidth() - lb.getWidth());
                }
                currentfile = lb.getF();
                ImageThumbnail lbold = (ImageThumbnail) thumbnailbottom.getComponent(indexold);
                lbold.setBorder(Constants.borderUnSelected);
                lb.setBorder(Constants.borderSelected);
                if (currentfile == null) {
                    return;
                }
                BufferedImage iBI = null;
                try {
                    iBI = ImageIO.read(currentfile);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                imgW = iBI.getWidth();
                imgH = iBI.getHeight();

                BufferedImage imgnew = iBI;
                if (zoom != 1) {
                    imgnew = Library.Resize(iBI, imgW, imgH, zoom);
                } else if (angle != 0) {
                    imgnew = Library.Rotate(iBI, imgW, imgH, angle);
                } else {
                    imgnew = iBI;
                }

                ImageView a = new ImageView(currentfile, new ImageIcon((Image) imgnew));
                a.setCursor(new Cursor(Cursor.HAND_CURSOR));
                a.addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent ev) {
                        if (ev.isPopupTrigger()) {
                            PopupMenu menutrip;
                            menutrip = new PopupMenu(currentfile);
                            menutrip.show(ev.getComponent(), ev.getX(), ev.getY());
                        }
                    }

                    

                    public void mouseReleased(MouseEvent ev) {
                        if (ev.isPopupTrigger()) {
                            PopupMenu menutrip;
                            menutrip = new PopupMenu(currentfile);
                            menutrip.show(ev.getComponent(), ev.getX(), ev.getY());
                        }
                    }
                });
                viewframe = new JScrollPane(a);
                int _x = screen.getX();
                int _y = screen.getY();
                int _W = screen.getWidth();
                int _H = screen.getHeight();
                screen.setLeftComponent(viewframe);
            }
        };
        t.start();
        ArrayThread.add(t);
    }

    protected void ShowImageInToThumnail(File directory) throws IOException {
        int _x = screen.getX();
        int _y = screen.getY();
        int _W = screen.getWidth();
        int _H = screen.getHeight();
        int count = 0;
        if (directory == null) {
            return;
        }
        FilePath = directory;
        thumbnailbottom.removeAll();
        thumbnailbottom.validate();
        
        screen.setDividerLocation(0.85);
        int x = thumnail_X;
        int y = thumainl_Y;
        index = 0;
        indexold = 0;
        for (File f : directory.listFiles()) {
            if (Library.isImage(f.getPath())) {
                ImageIcon ii = new ImageIcon(f.getPath());
                BufferedImage imgroot = ImageIO.read(f);
                int W, H;
                float scalethum = 1;
                float tile = ii.getIconWidth() > Constants.thumnail_W
                        ? (float) (Constants.thumnail_W * 1.0 / ii.getIconWidth())
                        : (float) (ii.getIconWidth() * 1.0 / Constants.thumnail_W);
                W = (int) Math.ceil(ii.getIconWidth() * tile);
                H = (int) Math.ceil(ii.getIconHeight() * tile);
                BufferedImage in = Library.Resize(imgroot, W, H, 1);
                ImageThumbnail btnthumnail = new ImageThumbnail(f, new ImageIcon((Image) in));

                btnthumnail.setBounds(x, y, Constants.thumnail_W, Constants.thumnail_H);
                x = x + Constants.thumnail_W + Constants.split;
                btnthumnail.setName(count + "");
                if (count == index)// current select
                {
                    btnthumnail.setBorder(Constants.borderSelected);
                    BufferedImage iBI = null;
                    try {
                        iBI = ImageIO.read(f);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    float Height=(float)screen.getHeight()*(float)0.9;
                    float _zom=1;
                    if(Height<iBI.getHeight()){
                        _zom=iBI.getHeight()/Height;
                    }
                    BufferedImage imgnew = Library.Resize(iBI, ii.getIconWidth(), ii.getIconHeight(),_zom);
                    ImageView a = new ImageView(f, new ImageIcon((Image) imgnew));//(lb.getIcon()));
                    viewframe = new JScrollPane(a);
                    viewframe.setLayout(null);
                    screen.setLeftComponent(viewframe);
                    screen.repaint();
                } else {
                    btnthumnail.setBorder(Constants.borderUnSelected);
                }
                count++;
                btnthumnail.setCursor(new Cursor(HAND_CURSOR));
                btnthumnail.setPreferredSize(new Dimension(Constants.thumnail_W, Constants.thumnail_H));
                btnthumnail.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        ImageThumbnail lb = (ImageThumbnail) e.getSource();
                        indexold = index;
                        index = Integer.parseInt(lb.getName());
                        currentfile = lb.getF();
                        DrawImage();
                    }
                });
                thumbnailbottom.add(btnthumnail);
                thumbnailbottom.repaint();
            }
        }
        thumbnailbottom.repaint();
        thumbnailbottom.setSize(_W, getInt(_H * 0.1));
        thumbnailbottom.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
    }
     protected void ShowImageFindInToThumnail(ArrayList<File> arr) throws IOException {
        int _x = screen.getX();
        int _y = screen.getY();
        int _W = screen.getWidth();
        int _H = screen.getHeight();
        int count = 0;
       
        thumbnailbottom.removeAll();
        screen.setDividerLocation(0.85);
        int x = thumnail_X;
        int y = thumainl_Y;
        index = 0;
        indexold = 0;
        for ( int i=0;i<arr.size();i++) {
                File f=arr.get(i);
                ImageIcon ii = new ImageIcon(f.getPath());
                BufferedImage imgroot = ImageIO.read(f);
                int W, H;
                float scalethum = 1;
                float tile = ii.getIconWidth() > Constants.thumnail_W
                        ? (float) (Constants.thumnail_W * 1.0 / ii.getIconWidth())
                        : (float) (ii.getIconWidth() * 1.0 / Constants.thumnail_W);
                W = (int) Math.ceil(ii.getIconWidth() * tile);
                H = (int) Math.ceil(ii.getIconHeight() * tile);
                BufferedImage in = Library.Resize(imgroot, W, H, 1);
                ImageThumbnail btnthumnail = new ImageThumbnail(f, new ImageIcon((Image) in));

                btnthumnail.setBounds(x, y, Constants.thumnail_W, Constants.thumnail_H);
                x = x + Constants.thumnail_W + Constants.split;
                btnthumnail.setName(count + "");
                if (count == index)// current select
                {
                    btnthumnail.setBorder(Constants.borderSelected);
                    BufferedImage iBI = null;
                    try {
                        iBI = ImageIO.read(f);
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    BufferedImage imgnew = Library.Resize(iBI, ii.getIconWidth(), ii.getIconWidth(), 1);
                    ImageView a = new ImageView(f, new ImageIcon((Image) imgnew));//(lb.getIcon()));
                    viewframe = new JScrollPane(a);
                    viewframe.setLayout(null);
                    screen.setLeftComponent(viewframe);
                    screen.repaint();
                } else {
                    btnthumnail.setBorder(Constants.borderUnSelected);
                }
                count++;
                btnthumnail.setCursor(new Cursor(HAND_CURSOR));
                btnthumnail.setPreferredSize(new Dimension(Constants.thumnail_W, Constants.thumnail_H));
                btnthumnail.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        ImageThumbnail lb = (ImageThumbnail) e.getSource();
                        indexold = index;
                        index = Integer.parseInt(lb.getName());
                        currentfile = lb.getF();
                        DrawImage();
                    }
                });
                thumbnailbottom.add(btnthumnail);
                thumbnailbottom.repaint();
            
        }
        thumbnailbottom.repaint();
        thumbnailbottom.setSize(_W, getInt(_H * 0.1));
//        System.out.print("Trên :"+screen.getRightComponent().getHeight());
//        System.out.print("Dưới :"+screen.getLeftComponent().getHeight());
        thumbnailbottom.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 1));
    }

    protected void Next() {
        indexold = index;
        index = (index == thumbnailbottom.getComponentCount() - 1) ? 0 : index + 1;
        DrawImage();
    }

    protected void Previous() {
        indexold = index;
        index = index == 0 ? thumbnailbottom.getComponentCount() - 1 : index - 1;
        DrawImage();
    }

    protected void ZoomIn() {
        zoom = (zoom - Constants.scaleperonce);
        zoom = zoom < 0 ? zoom - Constants.scaleperonce : zoom;
        DrawImage();
    }

    protected void ZoomOut() {
        zoom = Constants.scaleperonce + zoom;
        DrawImage();
    }

    protected void RotateAntiClockwise() {
        angle = angle - Constants.rotate;
        DrawImage();
    }

    protected void RotateClockwise() {
        angle = angle + Constants.rotate;// < 0 ? (rotate - Constants.rotate) * -1 : rotate - Constants.rotate;
        DrawImage();
    }

    protected void LoadAbums() throws IOException {
        Abums itemModel = new Abums();
        //jListAbum =new JList(itemModel);
        
        jListAbums.removeAll();
        jListAbums.setModel(itemModel);
        AbumsList = Abum.GetList();
        for (int i = 0; i < AbumsList.size(); i++) {
            itemModel.add(i, AbumsList.get(i));
        }
        jListAbums.repaint();
        Constants.flagLoadAbums = false;
    }

    public int getInt(double value) {
        return (int) Math.ceil(value);
    }

    public static void main(String args[]) throws ClassNotFoundException {

        try {
            String Theme = "com.jtattoo.plaf.mcwin.McWinLookAndFeel";
            javax.swing.UIManager.setLookAndFeel(Theme);
        } catch (Exception e) {
            System.out.println("An Exception occured: " + e);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree Jtreedisk;
    private javax.swing.JButton btnFullScreen;
    private javax.swing.JButton btnFullScreenPressKey;
    private javax.swing.JButton btnNewAbum;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPlay;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnRefeshAbum;
    private javax.swing.JButton btnRotate_Unclockwise;
    private javax.swing.JButton btnRotate_clockwise;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnZoomIn;
    private javax.swing.JButton btnZoomOut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jListAbums;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JButton jbtnEmail;
    private javax.swing.JButton jbtnPrint;
    private javax.swing.JButton jbtnTimkiem;
    private javax.swing.JMenuItem jmnFulscreen;
    private javax.swing.JMenuItem jmnabout;
    private javax.swing.JMenuItem jmncat;
    private javax.swing.JMenuItem jmncopy;
    private javax.swing.JMenuItem jmndelete;
    private javax.swing.JMenuItem jmnimport;
    private javax.swing.JMenuItem jmnnewalbum;
    private javax.swing.JMenuItem jmnprint;
    private javax.swing.JMenu jmnselectall;
    private javax.swing.JMenuItem jmnsendEmail;
    private javax.swing.JSplitPane screen;
    private javax.swing.JScrollPane screenLeft;
    private javax.swing.JSplitPane screenLeftMain;
    private javax.swing.JScrollPane screenLeftMainBottom;
    private javax.swing.JScrollPane screenLeftMainTop;
    private javax.swing.JSplitPane screenmain;
    private java.awt.TextField txtKeySearche;
    // End of variables declaration//GEN-END:variables
}
